package com.zmn.mcc.admin.controller.dept.dpermit;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.cube.common.dro.measure.MeasureDRO;
import com.zmn.cube.common.dro.menu.MenuDRO;
import com.zmn.cube.dubbo.dto.DubboConsts;
import com.zmn.cube.dubbo.interfaces.measure.MeasureListRemoteService;
import com.zmn.cube.dubbo.interfaces.menu.MenuListRemoteService;
import com.zmn.mcc.common.constant.DpermitConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:50
 */
@Controller
@RequestMapping("/dept/dpermit/modify/")
@Slf4j
public class DeptDpermitModifyController extends DeptDpermitBaseController {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MenuListRemoteService menuListRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MeasureListRemoteService measureListRemoteService;

    // region 组织数据权限

    /**
     * 为指定部门分配平台/大区/子公司数据权限
     */
    @RequestMapping("organize/assign")
    @ResponseBody
    public ResponseDTO assignOrganize(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                      @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO result = deptDpermitBService.assignOrganizeDpermit(deptId, StringUtil.split(dpermitIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 为指定部门添加服务商数据权限
     */
    @RequestMapping("organize/add")
    @ResponseBody
    public ResponseDTO addOrganize(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "keyId", defaultValue = "0") Integer deptId,
                                   @RequestParam(value = "companyIds", defaultValue = "") String companyIds) {

        ResponseDTO result = deptDpermitBService.addOrganizeDpermitForSp(deptId, StringUtil.split(companyIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定部门的指定子公司及其服务商数据权限
     */
    @RequestMapping("organize/delete")
    @ResponseBody
    public ResponseDTO deleteOrganize(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                      @RequestParam(value = "dataId", defaultValue = "0") Integer dataId) {

        ResponseDTO result = deptDpermitBService.deleteOrganizeDpermit(deptId, dataId, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定部门的指定服务商数据权限
     */
    @RequestMapping("organize/deleteSp")
    @ResponseBody
    public ResponseDTO deleteSpOrganize(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                        @RequestParam(value = "dataId", defaultValue = "0") Integer dataId) {

        ResponseDTO result = deptDpermitBService.deleteOrganizeDpermitForSp(deptId, dataId, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部组织数据权限
     */
    @RequestMapping("organize/clear")
    @ResponseBody
    public ResponseDTO clearOrganize(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearOrganizeDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部组织数据权限
     */
    @RequestMapping("promotion/clear")
    @ResponseBody
    public ResponseDTO clearAccount(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearAccountDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    // region 部门数据权限

    /**
     * 为指定部门分配部门数据权限
     */
    @RequestMapping("dept/assign")
    @ResponseBody
    public ResponseDTO assignDept(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO result = deptDpermitBService.assignDeptDpermit(deptId, StringUtil.split(dpermitIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部部门数据权限
     */
    @RequestMapping("dept/clear")
    @ResponseBody
    public ResponseDTO clearDept(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearDeptDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    // region 区域数据权限

    /**
     * 为指定部门分配区域数据权限
     */
    @RequestMapping("area/assign")
    @ResponseBody
    public ResponseDTO assignArea(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO result = deptDpermitBService.assignAreaDpermit(deptId, StringUtil.split(dpermitIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部区域数据权限
     */
    @RequestMapping("area/clear")
    @ResponseBody
    public ResponseDTO clearArea(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearAreaDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    // region 渠道数据权限

    /**
     * 为指定部门分配下单平台数据权限
     */
    @RequestMapping("channel/assignPlat")
    @ResponseBody
    public ResponseDTO assignChannelPlat(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                         @RequestParam(value = "dpermitIds", defaultValue = "") String plats) {

        ResponseDTO result = deptDpermitBService.assignChannelDpermitForPlat(deptId, StringUtil.split(plats, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 为指定部门添加渠道数据权限
     */
    @RequestMapping("channel/add")
    @ResponseBody
    public ResponseDTO addChannel(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "keyId", defaultValue = "0") Integer deptId,
                                  @RequestParam(value = "channelIds", defaultValue = "") String channelIds) {

        ResponseDTO result = deptDpermitBService.addChannelDpermit(deptId, StringUtil.split(channelIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定部门的指定平台及其渠道数据权限
     */
    @RequestMapping("channel/deletePlat")
    @ResponseBody
    public ResponseDTO deleteChannelPlat(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                         @RequestParam(value = "dataId", defaultValue = "0") Integer plat) {

        ResponseDTO result = deptDpermitBService.deleteChannelDpermit(deptId, plat, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定部门的指定渠道数据权限
     */
    @RequestMapping("channel/delete")
    @ResponseBody
    public ResponseDTO deleteChannel(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                     @RequestParam(value = "dataIds", defaultValue = "") String channelIds) {

        ResponseDTO result = deptDpermitBService.deleteChannelDpermit(deptId, StringUtil.split(channelIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部渠道数据权限
     */
    @RequestMapping("channel/clear")
    @ResponseBody
    public ResponseDTO clearChannel(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearChannelDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    // region 大数据报表数据权限

    /**
     * 为指定部门分配大数据报表数据权限
     */
    @RequestMapping("cube/assign")
    @ResponseBody
    public ResponseDTO assignCube(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO<List<MenuDRO>> menusResponse = menuListRemoteService.listAboveByLevel(DpermitConsts.CUBE_LEVEL_FOUR, GlobalDict.STATUS_ENABLE);
        // log.info("四级菜单为：{}",JSON.toJSONString(menusResponse));
        if (!menusResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得菜单列表");
        }
        ResponseDTO result = deptDpermitBService.assignCubeDpermit(deptId, StringUtil.split(dpermitIds, Integer.class), menusResponse.getData(), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部大数据报表数据权限
     */
    @RequestMapping("cube/clear")
    @ResponseBody
    public ResponseDTO clearCube(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearCubeDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    /**
     * 为指定部门分配大数据报表指标数据权限
     */
    @RequestMapping("measure/assign")
    @ResponseBody
    public ResponseDTO assignMeasure(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                     @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO<List<MeasureDRO>> measureResponse = measureListRemoteService.listMeasureByDataset(GlobalDict.STATUS_ENABLE);
        if (!measureResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得指标列表");
        }
        ResponseDTO result = deptDpermitBService.assignMeasureDpermit(deptId, StringUtil.split(dpermitIds, Integer.class), measureResponse.getData(), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部大数据报表指标数据权限
     */
    @RequestMapping("measure/clear")
    @ResponseBody
    public ResponseDTO clearMeasure(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearMeasureDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }

    // region 产品数据权限

    /**
     * 为指定部门分配产品数据权限
     */
    @RequestMapping("product/assign")
    @ResponseBody
    public ResponseDTO assignProduct(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                                     @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO result = deptDpermitBService.assignProductDpermit(deptId, StringUtil.split(dpermitIds, String.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部产品数据权限
     */
    @RequestMapping("product/clear")
    @ResponseBody
    public ResponseDTO clearProduct(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.clearProductDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
    // endregion

    /**
     * 为指定部门添加推广数据权限
     */
    @RequestMapping("promotion/add")
    @ResponseBody
    public ResponseDTO addPromotion(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,
                                    @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds,
                                    @RequestParam(value = "account", defaultValue = "") String account,
                                    @RequestParam(value = "type", defaultValue = "") Integer type) {

        ResponseDTO result = deptDpermitBService.addPromotionDpermit(keyId, StringUtil.split(dpermitIds, Integer.class), StringUtil.split(account, Integer.class), getStaffId(), getStaffName(), type);
        return result;
    }

    /**
     * 为指定部门添加账户渠道权限
     */
    @RequestMapping("newchannel/add")
    @ResponseBody
    public ResponseDTO addNewChannel(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        ResponseDTO result = deptDpermitBService.addNewChannelDpermit(keyId, StringUtil.split(dpermitIds, Integer.class),getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定部门的全部渠道数据权限
     */
    @RequestMapping("newchannel/clear")
    @ResponseBody
    public ResponseDTO clearNewChannel(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {
        ResponseDTO result = deptDpermitBService.clearNewChannelDpermit(deptId, getStaffId(), getStaffName());
        return result;
    }
}
